package descentFly.away3D4
{
	import away3d.containers.ObjectContainer3D;
	import away3d.core.base.Geometry;
	import away3d.core.base.Object3D;
	import away3d.core.base.SubGeometry;
	import away3d.entities.Mesh;
	
	import descentFly.jiglib.utils;
	
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;

	public class df_MeshHelper {
		public static function transform(mat: Matrix3D, mesh: Mesh): void {
			var geometry:Geometry = mesh.geometry;
			var geometries:Vector.<SubGeometry> = geometry.subGeometries;
			var numSubGeoms:int = geometries.length;
			var vertices:Vector.<Number>;
			var verticesLength: uint;
			var j: uint;
			var subGeom:SubGeometry;
			
			for (var i :uint = 0; i<numSubGeoms; ++i){
				subGeom = SubGeometry(geometries[i]);
				vertices = subGeom.vertexData;
				verticesLength = vertices.length;
				
				for (j = 0; j<verticesLength; j+=3){
					var v: Vector3D = new Vector3D(vertices[j], vertices[j + 1], vertices[j + 2]);
					v = mat.transformVector(v);
					vertices[j] 	= v.x;
					vertices[j+1] 	= v.y;
					vertices[j+2] 	= v.z;
				}
				
				subGeom.updateVertexData(vertices);
			}			
		}
		
		public static function childByName(obj: ObjectContainer3D, name: String): ObjectContainer3D {
			var child	: ObjectContainer3D;
			for (var i:int=0; i<obj.numChildren; i++) {
				child = obj.getChildAt(i);
				if (child.originalName == name)
					return child;
			}			
			return null;
		}
		
		public static function scaleObject(obj: ObjectContainer3D, scale: Number): void {
			var lmat	: Matrix3D;
			var child	: ObjectContainer3D;
			
			for (var i:int=0; i<obj.numChildren; i++) {
				child = obj.getChildAt(i);
				
				lmat = child.transform.clone();
				lmat.appendScale(scale, scale, scale);
				child.transform = lmat;		
			}
		}
	}
}